package com.yoomiito.app.utils

import android.Manifest
import android.R
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.util.TypedValue
import android.view.View
import android.view.WindowManager
import com.yoomiito.app.base.App
import java.lang.Exception


object DeviceConfigUtils {
    fun cpu(){
        val cpu = android.os.Build.SUPPORTED_ABIS
        cpu.forEach { it ->
            L.i("架构：$it")

        }

        val cpu1 = android.os.Build.CPU_ABI
        val cpu2 = android.os.Build.CPU_ABI2
        L.i("架构2：$cpu1")
        L.i("架构3：$cpu2")

        val cpu3 = android.os.Build.SUPPORTED_32_BIT_ABIS
        val cpu4 = android.os.Build.SUPPORTED_64_BIT_ABIS
        cpu3.forEach {
            L.i("架构4：$it")
        }
        cpu4.forEach {
            L.i("架构5：$it")
        }

    }

    fun hideStatusBar(activity: Activity) {
//        if (Build.VERSION.SDK_INT < 16) {
            activity.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN)
//        } else {
//            val decorView = activity.window.decorView
//            // Hide Status Bar.
//            val uiOptions: Int = View.SYSTEM_UI_FLAG_FULLSCREEN
//            decorView.systemUiVisibility = uiOptions
//        }
    }

    fun showStatusBar(activity: Activity) {
        if (Build.VERSION.SDK_INT < 16) {
            activity.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        } else {
            val decorView: View = activity.window.decorView
            // Show Status Bar.
            val uiOptions: Int = View.SYSTEM_UI_FLAG_VISIBLE
            decorView.systemUiVisibility = uiOptions
        }
    }

    fun getStatusBarHeight(context: Context): Int {
        var result = 0
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    fun getActionBarHegith(context: Context): Int {
        val tv = TypedValue()
        return if (context.theme.resolveAttribute(R.attr.actionBarSize, tv, true)) {
            TypedValue.complexToDimensionPixelSize(tv.data, context.resources.displayMetrics)
        } else 0
    }

    fun callTel(context: AppCompatActivity, phoneNum:String){

//        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.M)
//        AllUtils.requestPermission(context, Manifest.permission.CALL_PHONE).subscribe {
//            if (it){
//                call(context, phoneNum)
//            }
//        }else{
//            call(context, phoneNum)
//        }


    }

    private fun call(context: Activity,phoneNum:String){
        val intent =  Intent(Intent.ACTION_CALL)
        val data = Uri.parse(if (phoneNum.startsWith("tel:")) phoneNum else "tel:$phoneNum")
        intent.data = data
        context.startActivity(intent)
    }

    fun inputTelNum(context: Activity,phoneNum:String){
        val intent =  Intent(Intent.ACTION_DIAL)
        val data = Uri.parse(if (phoneNum.startsWith("tel:")) phoneNum else "tel:$phoneNum")
        intent.data = data
        context.startActivity(intent)
    }

    /**
     * 根据包名判断某个app是否安装
     * @param packageName
     * @return
     */
    fun isAppInstalled(packageName:String):Boolean{
        return try {
            var packageInfo:PackageInfo = App.getInstance().packageManager.getPackageInfo(packageName, 0)
            packageInfo != null
        }catch (e:Exception){
            false
        }
    }

}